package com.qst.interceptor;

import com.qst.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginInterceptor implements HandlerInterceptor {


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
//放行的情况
        if(uri.indexOf("toLogin")!=-1||uri.indexOf("login")!=-1||uri.indexOf("login.jsp")!=-1||uri.indexOf("getVerifyCode")!=-1
                || uri.indexOf("toRegister")!=-1  || uri.indexOf("register")!=-1 || uri.indexOf("getRoleList")!=-1
                || uri.indexOf("toUpload")!=-1 || uri.indexOf("upload")!=-1|| uri.indexOf("uploadFile")!=-1){

            return true;

        }
//不放行的情况，
        User userInfo = (User) request.getSession().getAttribute("userInfo");
//放行的条件，没有userInfo跳转登入页面
        if(userInfo==null){

            request.getRequestDispatcher("/user/toLogin").forward(request,response);

            return false;
//有userInfo放行
        }else {

            return true;

        }

    }
}
